/**
 * 
 */
package com.macbulldev.poc.testgin.jobs;

import javax.servlet.ServletContext;

import net.youngdev.util.Context;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.macbulldev.poc.testgin.service.EmailService;



/**
 * @author solid
 *
 */
@Component("notifyUserApprovedJob")
@Configurable
public class NotifyUserStatusUpdateJob extends QuartzJobBean 
{
	public static final String USER_EMAIL_KEY="USER_EMAIL";
	public static final String SERVER_URL_BASE_KEY="SERVER_URL_BASE";
	public static final String ENABLED_BOL_KEY = "ENABLED_BOL";

	private static final Logger LOGGER=Logger.getLogger(NotifyUserStatusUpdateJob.class);


	@Autowired
	@Qualifier("emailMessageSource")
	private ReloadableResourceBundleMessageSource emailMessages;

	@Autowired
	@Qualifier("emailService")
	private EmailService emailService;


	@Override
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
		String userEmail = (String)context.getJobDetail().getJobDataMap().get(NotifyUserStatusUpdateJob.USER_EMAIL_KEY);
		Boolean enabled=(Boolean)context.getJobDetail().getJobDataMap().get(NotifyUserStatusUpdateJob.ENABLED_BOL_KEY);
		ServletContext ctx =Context.getInstance().getContext();
		//ctx.getAttributeNames();
		String serverBase = (String)ctx.getAttribute("APPLICATION_BASE_URL");

		String loginURL = serverBase+ctx.getContextPath()+"/login";

		LOGGER.error("login URL ="+loginURL);
		context.setResult("Sending email to "+userEmail+" enabled = "+enabled);

		try
		{
			LOGGER.debug("User is being enabled "+userEmail+ " "+enabled);
			if (enabled!=null)
			{
				if (enabled)
				{
					String subject = emailMessages.getMessage("email.template.account.approved.subject", null, null);
					String body = emailMessages.getMessage("email.template.account.approved.body", new Object[]{userEmail,loginURL}, null);
					emailService.sendEmail(new String[]{userEmail}, subject, body);
					
					subject = emailMessages.getMessage("email.template.account.welcome.subject", null, null);
					body = emailMessages.getMessage("email.template.account.welcome.body", new Object[]{userEmail}, null);
					emailService.sendEmail(new String[]{userEmail}, subject, body);
					
					
				}
				else
				{
					String admin = emailMessages.getMessage("application.admin.email",null,null);
					String subject = emailMessages.getMessage("email.template.account.disabled.subject", null, null);
					String body = emailMessages.getMessage("email.template.account.disabled.body", new Object[]{userEmail,admin}, null);
					emailService.sendEmail(new String[]{userEmail}, subject, body);

				}
				context.setResult("Sending email to "+userEmail+" complete.");
			}
			Thread.sleep(15000L);


		}
		catch (Exception e)
		{
			LOGGER.error("Exception occured while notifying "+userEmail,e);
			context.setResult("Sending email to "+userEmail+" failed.  please review logs. e.message="+e.getMessage());

		}


	}


	public ReloadableResourceBundleMessageSource getEmailMessages() {
		return emailMessages;
	}


	public void setEmailMessages(ReloadableResourceBundleMessageSource emailMessages) {
		this.emailMessages = emailMessages;
	}


	public EmailService getEmailService() {
		return emailService;
	}


	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

}
